package com.cloudeasy.label.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudeasy.api.enums.ResultCodeEnum;
import com.cloudeasy.api.exception.CloudEasyException;
import com.cloudeasy.label.constant.LabelConstant;
import com.cloudeasy.label.mapper.LabelAttentionMapper;
import com.cloudeasy.label.po.Label;
import com.cloudeasy.label.po.LabelAttention;
import com.cloudeasy.label.util.IPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LabelAttentionService extends ServiceImpl<LabelAttentionMapper, LabelAttention> {

    private final LabelService labelService;

    @Autowired
    public LabelAttentionService(LabelService labelService) {
        this.labelService = labelService;
    }

    /**
     * 根据用户Id查询记录
     *
     * @param userId 用户Id
     * @return {@link List<LabelAttention>}
     */
    public List<LabelAttention> listByUserId(String userId) {
        QueryWrapper<LabelAttention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.list(queryWrapper);
    }

    /**
     * 根据标签查询记录
     *
     * @param id Id
     * @return {@link List<LabelAttention>}
     */
    public List<LabelAttention> listById(String id) {
        QueryWrapper<LabelAttention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("label_id", id);
        return this.list(queryWrapper);
    }

    /**
     * 根据标签分页查询记录
     *
     * @param id   项Id
     * @param page 页数
     * @param size 每页大小
     * @return {@link IPage<LabelAttention>}
     */
    public IPage<LabelAttention> pageById(String id, Integer page, Integer size) {
        QueryWrapper<LabelAttention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("label_id", id);
        IPage<LabelAttention> labelAttentionIPage = IPageUtil.compose(page, size);
        return this.page(labelAttentionIPage, queryWrapper);
    }

    /**
     * 根据标签Id删除记录
     *
     * @param id Id
     */
    public void removeById(String id) {
        this.remove(wrapper(null, id));
    }

    /**
     * 关注
     *
     * @param userId 用户Id
     * @param id     Id
     * @return {@link Boolean}
     */
    public Boolean attention(String userId, String id) {
        Label label = labelService.getById(id);
        if (label == null) {
            throw new CloudEasyException(ResultCodeEnum.ID_NOT_EXISTS);
        }
        if (label.getStatus().equals(LabelConstant.FORBID_ATTENTION)) {
            throw new CloudEasyException(ResultCodeEnum.FORBID_ATTENTION);
        }
        if (this.getOne(wrapper(userId, id)) != null) {
            throw new CloudEasyException(ResultCodeEnum.REPEAT_ERROR);
        }
        LabelAttention labelAttention = LabelAttention.builder().userId(userId).labelId(id).build();

        labelService.attention(id);
        return this.save(labelAttention);
    }

    /**
     * 移除关注
     *
     * @param userId 用户Id
     * @param id     Id
     * @return {@link Boolean}
     */
    public Boolean removeAttention(String userId, String id) {
        if (this.getOne(wrapper(userId, id)) == null) {
            throw new CloudEasyException(ResultCodeEnum.REPEAT_ERROR);
        }

        labelService.removeAttention(id);
        return this.remove(wrapper(userId, id));
    }

    /**
     * 构建wrapper
     *
     * @param userId 用户Id
     * @param id     Id
     * @return {@link QueryWrapper<LabelAttention>}
     */
    public QueryWrapper<LabelAttention> wrapper(String userId, String id) {
        QueryWrapper<LabelAttention> queryWrapper = new QueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        if (id != null) {
            queryWrapper.eq("label_id", id);
        }
        return queryWrapper;
    }
}
